package com.wanwen.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanwen.dto.ModuleDto;
import com.wanwen.entity.Modules;
import com.wanwen.mapper.ModuleMapper;
import com.wanwen.utils.CommonUtil;
import com.wanwen.utils.Result;
import com.wanwen.utils.UITreeFactory;

@RestController
@RequestMapping("/module")
public class ModuleController {
	@Autowired
	private ModuleMapper moduleMapper;

	/**
	 * 查询所有模块
	 * 
	 * @return layui加载树形组件需要的json数据
	 */
	@RequestMapping("/getAll")
	public Object getAll() {
		QueryWrapper<Modules> wrapper = new QueryWrapper<Modules>();
		wrapper.orderByAsc("weight");
		List<Modules> list = moduleMapper.selectList(wrapper);// 获取所有的模块

		List<ModuleDto> mdtoList = new ArrayList<ModuleDto>();// 所有的已转的模块业务对象
		for (Modules ms : list) {
			mdtoList.add(CommonUtil.getModuleDtoByModule(ms));
		}
		UITreeFactory.loadTree(mdtoList);
		return UITreeFactory.moduleTreeList;
	}

	/**
	 * 根据Id查询模块
	 * 
	 * @return
	 */
	@RequestMapping("/getById")
	public Object getById(String id) {
		QueryWrapper<Modules> wrapper = new QueryWrapper<Modules>();
		wrapper.eq("id", id);
		Modules Modules = moduleMapper.selectOne(wrapper);
		return Modules;
	}

	/**
	 * 根据查询所有父模块
	 * 
	 * @return
	 */
	@RequestMapping("/getAllParent")
	public Object getAllParent() {
		QueryWrapper<Modules> wrapper = new QueryWrapper<Modules>();
		wrapper.eq("parent_id", 0);
		List<Modules> list = moduleMapper.selectList(wrapper);
		return list;
	}

	/**
	 * /module/add 添加模块
	 * 
	 * @param module
	 * @return rs
	 */
	@RequestMapping("/add")
	public Object add(Modules module) {
		QueryWrapper<Modules> wrapperone = new QueryWrapper<Modules>();
		wrapperone.eq("mname", module.getMnane());
		Modules moduleOne = moduleMapper.selectOne(wrapperone);
		if (moduleOne == null) {
			int r = moduleMapper.insert(module);
			Result rs = new Result(r == 1 ? "成功" : "失败", "", r == 1 ? true : false);
			return rs;
		} else {
			Result rs = new Result("失败:模块名已存在", "", false);
			return rs;
		}
	}

	/**
	 * /module/add 添加父模块
	 * 
	 * @param module
	 * @return
	 */
	@RequestMapping("/addParent")
	public Object addParent(Modules module) {
		QueryWrapper<Modules> wrapperone = new QueryWrapper<Modules>();
		wrapperone.eq("mname", module.getMnane());
		Modules moduleOne = moduleMapper.selectOne(wrapperone);
		if (moduleOne == null) {
			module.setParentId(0);
			int r = moduleMapper.insert(module);
			Result rs = new Result(r == 1 ? "成功" : "失败", "", r == 1 ? true : false);
			return rs;
		} else {
			Result rs = new Result("失败:模块名已存在", "", false);
			return rs;
		}
	}

	/**
	 * /module/update 修改模块
	 * 
	 * @param module
	 * @return rs
	 */
	@RequestMapping("/update")
	public Object update(Modules module) {
		QueryWrapper<Modules> wrapperone = new QueryWrapper<Modules>();
		wrapperone.eq("mname", module.getMnane());
		Modules moduleOne = moduleMapper.selectOne(wrapperone);
		if (moduleOne == null||moduleOne.getId()==module.getId()) {
			int r = moduleMapper.updateById(module);
			Result rs = new Result(r == 1 ? "成功" : "失败", "", r == 1 ? true : false);
			return rs;
		} else {
			Result rs = new Result("失败:模块名已存在", "", false);
			return rs;
		}
	}

	/**
	 * /module/del 删除模块
	 * 
	 * @param module
	 * @return rs
	 */
	@RequestMapping("/del")
	public Object del(Modules module) {
		QueryWrapper<Modules> wrapper = new QueryWrapper<Modules>();
		wrapper.eq("parent_id", module.getId());
		List<Modules> list = moduleMapper.selectList(wrapper);
		if (list.size() == 0) {
			int r = moduleMapper.deleteById(module);
			Result rs = new Result(r == 1 ? "成功" : "失败", "", r == 1 ? true : false);
			return rs;
		} else {
			Result rs = new Result("失败：模块下拥有子模块", "", false);
			return rs;
		}

	}
}
